import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.*;


public class MovementTest extends JPanel implements ActionListener {
	
	private Timer tim = new Timer(30, this);
	private final int max = 300;
	private int x1Speed = 0;
	private int y1Speed = 0;
	private int x2Speed = 0;
	private int y2Speed = 0;
	private int x1 = 150;
	private int y1 = 150;
	private int x2 = 850;
	private int y2 = 150;
	private int xMax = 350;
	private int yMax = 350;

	public MovementTest() {
		tim.start();
		addKeyListener(kl);
		setFocusable(true);

		
	}
	public void init() {
		addKeyListener(kl);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
			x1 = x1 + x1Speed;
			y1 = y1 + y1Speed;
			
			x2 = x2 + x2Speed;
			y2 = y2 + y2Speed;
		
		repaint();
		
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		g.setColor(Color.green);
		g.fillRect(100, 100, 50, 50);
		g.setColor(Color.red);
		g.fillOval(x1, y1, 20, 20);
		g.setColor(Color.blue);
		g.fillOval(x2, y2, 20, 20);
		
	}	
	
	KeyListener kl = new KeyAdapter() {
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_UP) {
				y2SpeedDec();
			}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
				y2SpeedInc();
			}
			
			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
				x2SpeedDec();
			}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
				x2SpeedInc();
			}
			
			if (e.getKeyCode() == KeyEvent.VK_S) {
				y1SpeedInc();
			}
			else if (e.getKeyCode() == KeyEvent.VK_W) {
				y1SpeedDec();
			}
			if (e.getKeyCode() == KeyEvent.VK_A) {
				x1SpeedDec();
			}
			else if (e.getKeyCode() == KeyEvent.VK_D) {
				x1SpeedInc();
			
			}
			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
				killRect();
			}
		}
	};
	
	private void killRect() {
		if (x1 > 100 && x1 < 150 && y1 > 100 && y1 < 150)   {
			System.out.println("HIT");
		}
		
	}
	//Player 1:
	
	private void x1SpeedInc() {
		if (x1Speed < 10) {
			x1Speed++;
		}
	}
	
	private void x1SpeedDec() {
		if (x1Speed > -10) {
			x1Speed--;
		}
	}
	
	private void y1SpeedInc() {
		if (y1Speed < 5) {
			y1Speed++;
		}
	}
	
	private void y1SpeedDec() {
		if (y1Speed > -5) {
			y1Speed--;
		}
	}
	
	//Player 2
	private void x2SpeedInc() {
		if (x2Speed < 10) {
			x2Speed++;
		}
	}
	
	private void x2SpeedDec() {
		if (x2Speed > -10) {
			x2Speed--;
		}
	}
	
	private void y2SpeedInc() {
		if (y2Speed < 5) {
			y2Speed++;
		}
	}
	
	private void y2SpeedDec() {
		if (y2Speed > -5) {
			y2Speed--;
		}
	}

}

